package com.mg.plugin.agrometeorology.common;


import com.mg.model.Model;
import com.mg.plugin.agrometeorology.entity.StationData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 日照时数计算
 */
public class SunshineCalculator {

    /**
     * 全省日平均日照时数计算
     *
     * @param stationHoursOfSunshines 站点日照时数
     * @return 全省日平均日照时数
     */
    private static float calcAvgHoursOfSunshineForDay(List<StationData> stationHoursOfSunshines) {
        if (stationHoursOfSunshines == null || stationHoursOfSunshines.size() == 0) {
            return 0;
        }
        float totalHoursOfSunshine = 0;
        for (StationData stationData : stationHoursOfSunshines) {
            totalHoursOfSunshine += stationData.getSunshine();
        }
        return totalHoursOfSunshine / stationHoursOfSunshines.size();
    }

    /**
     * 全省累积日照时数计算
     * 全省累积日照时数是利用全省日平均日照时数计算的逐日全省日平均日照时数的累积，一般从作物播种日期开始，
     * 至每个月的逢5日、10日、15日、20日、25日、30日(或28日、29日、31日)逐次计算，至收获日结束。
     *
     * @param stationDataList 全省日平均日照时数
     * @param i
     * @param m_model
     * @return 全省累积日照时数
     */
    public static float calcAccumulateHoursOfSunshine(Map<String, List<StationData>> stationDataList, int i, Model m_model) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        float accTemp = 0;
        for (String date : stationDataList.keySet()) {
            Date calcDate = DateCalculator.calcTime(sdf.parse(date), i);
            if (calcDate.before(sdf.parse(date))) {
                continue;
            }
            List<StationData> data = stationDataList.get(date);
            accTemp += calcAvgHoursOfSunshineForDay(data);
        }
        return accTemp;
    }

    public static float calcTendaysAccumulateHoursOfSunshine(Map<String, List<StationData>> stationDataList, int i, Model m_model) throws ParseException {
        if (i % 2 == 1) {
            return 999999.9f;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        float accTemp = 0;
        for (String date : stationDataList.keySet()) {
            Date calcDate = DateCalculator.calcTime(sdf.parse(date), i);
            Date beforeDate = DateCalculator.calcTendaysBeforeTime(sdf.parse(date), i);
            if (calcDate.before(sdf.parse(date)) || beforeDate.after(sdf.parse(date))) {
                continue;
            }
            List<StationData> data = stationDataList.get(date);
            accTemp += calcAvgHoursOfSunshineForDay(data);
        }
        return accTemp;
    }

    public static float calcTendaysSuitabilityHoursOfSunshine(Map<String, List<StationData>> stationDataList, Map<String, List<StationData>> oldStationDataList, int i, Model m_model) throws ParseException {
        if (i % 2 == 1) {
            return 999999.9f;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        float accTemp = 0;
        float oldAccTemp = 0;
        for (String date : stationDataList.keySet()) {
            Date calcDate = DateCalculator.calcTime(sdf.parse(date), i);
            Date beforeDate = DateCalculator.calcTendaysBeforeTime(sdf.parse(date), i);
            if (calcDate.before(sdf.parse(date)) || beforeDate.after(sdf.parse(date))) {
                continue;
            }
            List<StationData> data = stationDataList.get(date);
            List<StationData> oldData = oldStationDataList.get(date);
            accTemp += calcAvgHoursOfSunshineForDay(data);
            oldAccTemp += calcAvgHoursOfSunshineForDay(oldData);
        }
        return accTemp > oldAccTemp ? 1 : accTemp / oldAccTemp;
    }
}
